import com.mt.components.consts.PermissionName;
import com.mt.components.consts.SysConst;
import com.mt.components.log.LogFactory;
import com.mt.components.log.Logger;
import com.mt.components.utils.RemoteFactory;
import com.mt.system.domain.Permission;
import com.mt.system.domain.User;
import com.mt.system.view.UserView;
import com.mt.system.view.user.SaveUserView;

import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.core.FlexGlobals;
import mx.events.ItemClickEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;

/**      
 * 文件名称：UserViewAs.as<br>
 * 文件描述：<br>
 * 创建人：马腾<br>
 * 创建时间：Nov 25, 2012 7:43:35 PM<br>
 * 修改人：马腾<br>
 * 修改时间：Nov 25, 2012 7:43:35 PM<br>
 */

private static var logger:Logger = LogFactory.getLogger(UserView);

[Bindable]private var users:ArrayCollection;

public function submit():void {
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("UserService");
	remote.addEventListener(ResultEvent.RESULT , resultCount);
	remote.getCount(null);
}

protected function resultCount(event:ResultEvent):void {
	bar.setTotalRecords(event.result as int);
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("UserService");
	remote.addEventListener(ResultEvent.RESULT , resultData);
	remote.getUsers(null);
}

protected function resultData(event:ResultEvent):void {
	users = event.result as ArrayCollection;
}

protected function click(event:ItemClickEvent):void {
	var permission:Permission = event.item as Permission;
	if(permission.mark == PermissionName.USER_ADD){
		addUser();
	}else if(permission.mark == PermissionName.USER_DEL){
		deleteUser();
	}else if(permission.mark == PermissionName.USER_MOD){
		updateUser();
	}
}

private function deleteUser():void {
	var selectItems:Array = grid.selectedItems;
	if(!selectItems || selectItems.length == 0){
		msg.text = "请选中记录进行操作！";
		return;
	}
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("UserService" , PermissionName.USER_DEL);
	remote.addEventListener(ResultEvent.RESULT , function (e:ResultEvent):void {
		msg.text = "删除成功！";
		submit();
	});
	remote.deleteBatchUser(selectItems);
}

private function addUser():void {
	var saveUserView:SaveUserView = new SaveUserView();
	saveUserView.title = "增加用户";
	saveUserView.addEventListener(SysConst.SAVE_SUCCESS , saveUserSuccess);
	PopUpManager.addPopUp(saveUserView ,FlexGlobals.topLevelApplication.vk , true);
	PopUpManager.centerPopUp(saveUserView);
}

private function updateUser():void {
	if(!grid.selectedItem){
		msg.text = "请选中记录进行操作！";
		return;
	}
	var saveUserView:SaveUserView = new SaveUserView();
	saveUserView.title = "修改用户";
	saveUserView.user = grid.selectedItem as User;
	saveUserView.addEventListener(SysConst.SAVE_SUCCESS , saveUserSuccess);
	PopUpManager.addPopUp(saveUserView ,FlexGlobals.topLevelApplication.vk , true);
	PopUpManager.centerPopUp(saveUserView);
}

/**
 * 保存用户成功后 从新刷新用户展现界面 
 * @param event
 */
protected function saveUserSuccess(event:Event):void {
	submit();
}
